<template>
  <div class="com-container">
    <!--新增表单对话框-->
    <el-dialog
        title="编辑管理员信息"
        :visible.sync="dialogVisible"
        width="25%"
        center
    >
      <el-form ref="form" :model="admainInfo" label-width="auto" :inline="true">
        <el-form-item label="管理员账户名">
          <el-input v-model="admainInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="admainInfo.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取消</el-button>
    <el-button type="primary" @click="handleAdd">提交</el-button>
  </span>
    </el-dialog>
    <!--修改编辑对话框-->
    <el-dialog
        title="编辑管理员信息"
        :visible.sync="dialogEditVisible"
        width="25%"
        center
    >
      <el-form ref="form" :model="admainInfo" label-width="auto" :inline="true">
        <el-form-item label="账号名称">
          <el-input v-model="admainInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="账号密码">
          <el-input v-model="admainInfo.password"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogEditVisible = false">取消</el-button>
    <el-button type="primary" @click="handleEdit">确认修改</el-button>
  </span>
    </el-dialog>
    <!--新增按钮-->
    <el-row style="margin-bottom: 5px">
      <el-button type="primary" @click="handleAddButtonClick">新增</el-button>
    </el-row>
    <!--表格数据-->
    <el-table :data="tableData"
              style="width: 100%"
              stripe
              border
              max-height="500"
    >
      <el-table-column
          prop="id"
          label="id"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="username"
          label="管理员账号名"
          align="center"
      ></el-table-column>
      <el-table-column
          prop="password"
          label="密码"
          align="center"
      ></el-table-column>
      <el-table-column
          label="操作"
          align="center"
      >
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEditButtonClick(scope.row)">编辑
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDeleteButtonClick(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
export default {
  name: "AdmainInfo",
  mounted() {
    this.selectAll()
  },
  data() {
    return {
      tableData: [],
      admainInfo: {
        username: '',
        password: ''
      },
      dialogVisible: false,
      dialogEditVisible: false
    }
  },
  methods: {
    selectAll() {
      this.request.get("/admain").then(res => {
        if (res.code == 20041) {
          this.tableData = res.data
        } else if (res.code == 20040) {
          this.$message.error("查询失败")
        } else {
          this.$message.error("系统错误，请仔细检查")
        }
      })
    },
    handleAddButtonClick() {
      this.dialogVisible = true
    },
    handleAdd() {
      this.request.post("/admain", this.admainInfo).then(res => {
        if (res.code == 20011) {
          this.dialogVisible = false;
          this.$message.success("添加管理员账户成功");
        } else if (res.code == 20010) {
          this.$message.error("添加管理员账户失败");
        } else {
          // 此种情况下为系统错误或者运行异常,将返回异常返回的msg
          this.$message.error(res.msg);
        }
      }).finally(() => {
        this.selectAll()
      })
    },
    handleEditButtonClick(row) {
      this.request.get("/admain/" + row.id).then(res => {
        if (res.code == 20041) {
          this.admainInfo = res.data;
          this.dialogEditVisible = true;
        } else if (res.code == 20040) {
          this.$message.error("数据回显失败")
        } else {
          this.$message.error("系统错误,请检查代码")
        }
      })
    },
    handleDeleteButtonClick(row) {
      this.$confirm("此操作永久删除当前数据，是否继续？", "警告", {
        type: 'warning'
      }).then(() => {
        this.request.delete("/admain/" + row.id).then(res => {
          if (res.code == 20021) {
            this.$message.success("删除成功")
          } else if (res.code == 20020) {
            this.$message.error("删除失败")
          } else {
            this.$message.error(res.msg)
          }
        }).finally(() => {
          this.selectAll()
        })
      }).catch(() => {
        this.$message.info("已取消删除")
      })
    },
    handleEdit() {
      this.request.put("/admain", this.admainInfo).then(res => {
        if (res.code == 20031) {
          this.$message.success(res.msg)
          this.dialogEditVisible = false
        } else if (res.code == 20030) {
          this.$message.error(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).finally(()=>{
        this.selectAll()
      })

    }
  }
}
</script>

<style scoped>

</style>